package org.xb;

/**
 * @Author
 * @Description 懒汉式
 * @Date created in 10:05 2024/10/22
 */
public class Main03 {
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                Person3 instance = Person3.getInstance();
                System.out.println(instance.hashCode());
            }).start();
        }
    }
}

class Person3 {
    private Person3() {
    }

    private static Person3 person;

    public static Person3 getInstance() {
        if (person == null) {
            //由于没有进行Double check lock的 这里虽然是进行了锁 但是锁内还是没有检查
            synchronized (Person3.class) {
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                person = new Person3();
            }
        }
        return person;
    }
}


